package com.findviewbydream.evil.helloyouth.SlideMenu;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.findviewbydream.evil.helloyouth.Data.UserData;
import com.findviewbydream.evil.helloyouth.Fragment.Fragment1;
import com.findviewbydream.evil.helloyouth.Fragment.Fragment2;
import com.findviewbydream.evil.helloyouth.Fragment.HomeFragment;
import com.findviewbydream.evil.helloyouth.MainActivity;
import com.findviewbydream.evil.helloyouth.MainActivityUtils.MainActivityUtils;
import com.findviewbydream.evil.helloyouth.R;
import com.findviewbydream.evil.helloyouth.User.Login.Login;
import com.findviewbydream.evil.helloyouth.Utils.SLog;
import com.findviewbydream.evil.helloyouth.Welcome.Welcome;

import cn.iwgang.familiarrecyclerview.FamiliarRecyclerView;
import de.hdodenhof.circleimageview.CircleImageView;

/********************************************
 * 工程名：HelloYouth
 * 包名：com.findviewbydream.evil.helloyouth.LeftMenu
 * 时间：16-6-7 下午1:53
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：主界面左侧菜单Fragment
 ********************************************/
public class LeftFragment extends Fragment
{
    private View view;
    private static LeftFragment leftFragment;

    /*整体*/
    private ImageView top_background;
    private ImageView backgroud;
    private FamiliarRecyclerView recyclerView;

    /*顶部用户信息*/
    private CircleImageView userImg;
    private TextView userName;
    private TextView userNum;
    private TextView userIntegration;

    private Adapter adapter;
    private String[] list = {"首页", "订单", "关于"};

    /*Fragment管理*/
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private Fragment fragment;

    /**
     * 入口函数
     *
     * @param b
     * @return
     */
    public static LeftFragment newInstance(Bundle b)
    {
        if (leftFragment == null)
            leftFragment = new LeftFragment();
        leftFragment.setArguments(b);
        return leftFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.left_fragment, container, false);

        initView(view);
        initFragment();/*初始化Fragment*/
        initBackGround();/*设置背景*/
        setRecyclerView();/*设置RecyclerView*/
        onClick();/*点击事件*/
        return view;
    }

    /**
     * 初始化控件
     *
     * @param view
     */
    private void initView(View view)
    {
        top_background = (ImageView) view.findViewById(R.id.top_background);
        userImg = (CircleImageView) view.findViewById(R.id.userImg);
        userName = (TextView) view.findViewById(R.id.userName);
        userNum = (TextView) view.findViewById(R.id.userNum);
        userIntegration = (TextView) view.findViewById(R.id.userIntegration);
        recyclerView = (FamiliarRecyclerView) view.findViewById(R.id.recyclerView);
        backgroud = (ImageView) view.findViewById(R.id.backgroud);
    }

    /**
     * 初始化背景图片和用户头像
     */
    private void initBackGround()
    {
        Glide.with(getContext()).load(R.drawable.slidebar_background_top).into(top_background);
        Glide.with(getContext()).load(R.drawable.slidebar_background).into(backgroud);
        Glide.with(getContext()).load(R.drawable.userimage).into(userImg);

        if (UserData.userName == null)
            userName.setText("姓名：未设置");
        else
            userName.setText("姓名：" + UserData.userName);

        if (UserData.userName == null)
            userNum.setText("学号：未设置");
        else
            userNum.setText("学号：" + UserData.userNum);

        userIntegration.setText("积分：" + UserData.userIntegration + "");
    }


    /**
     * 初始化Fragment
     */
    private void initFragment()
    {
        /*Fragment管理初始化*/
        {
            fragmentManager = getFragmentManager();
            transaction = fragmentManager.beginTransaction();
        }

        /*默认Fragment*/
        if (fragment == null)
        {
            fragment = HomeFragment.newInstance();
            transaction.add(R.id.container_main, fragment).commit();
        }
    }

    /**
     * 判断fragment是否添加过,如果添加过,就隐藏然后显示,没添加过就添加
     *
     * @param from 旧的fragment
     * @param to   新的fragment
     */
    private void switchContent(Fragment from, Fragment to)
    {
        if (fragment != to)
        {
            fragment = to;
            if (!to.isAdded())
            {    // 先判断是否被add过
                //SLog.print("添加");
                transaction.hide(from).add(R.id.container_main, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
            }
            else
            {
                //SLog.print("替换");
                transaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }
        }
    }


    /**
     * 设置RecyclerView
     */
    private void setRecyclerView()
    {
        adapter = new Adapter();
        recyclerView.setAdapter(adapter);
    }


    /**
     * 点击事件
     */
    private void onClick()
    {
        /*用户头像点击，跳转到登陆或者详情页面*/

        userImg.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                if (UserData.userName == null)
                    startActivity(new Intent(getActivity(), Login.class));
                else
                    startActivity(new Intent(getActivity(), Welcome.class));
            }
        });


        recyclerView.setOnItemClickListener(new FamiliarRecyclerView.OnItemClickListener()
        {
            @Override
            public void onItemClick(FamiliarRecyclerView familiarRecyclerView
                    , View view, int position)
            {
                /*先将FragmentManager初始化*/
                initFragment();
                switch (position)
                {
                    case 0:
                        switchContent(fragment, HomeFragment.newInstance());
                        break;
                    case 1:
                        switchContent(fragment, Fragment1.newInstance());
                        break;
                    case 2:
                        switchContent(fragment, Fragment2.newInstance());
                        break;
                }
                MainActivityUtils.closeSlidebar();
            }
        });
    }


    /**
     * RecyclerView的Adapter
     */
    private class Adapter extends RecyclerView.Adapter<ViewHolder>
    {

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
        {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.left_fragment_item, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position)
        {
            holder.textView.setText(list[position]);
        }

        @Override
        public int getItemCount()
        {
            return list.length;
        }
    }

    /**
     * ViewHolder
     */
    private class ViewHolder extends RecyclerView.ViewHolder
    {
        TextView textView;
        ImageView imageView;

        public ViewHolder(View itemView)
        {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.textView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
        }
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        fragment = null;
    }
}
